"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createRoom = exports.sendObj = exports.numOfHallPlayers = exports.createServer = exports.map_room = exports.arr_hallPlayer = exports.VERSION = void 0;
const nodejs_websocket_1 = __importDefault(require("nodejs-websocket"));
const Player_1 = require("./Player");
const Room_1 = require("./Room");
exports.VERSION = "2022.8.13"; //版本号
let id_num = 1; //登录用户给一个id值，1-10000 递增就好
let roomId_num = 1; //房间给一个id值，1-10000 递增就好
const all_players = new Set(); //全部玩家
exports.arr_hallPlayer = new Set(); //在大厅的全部玩家
exports.map_room = new Map(); //所有房间
const createServer = () => {
    console.log("野棋后台服务器启动完成");
    nodejs_websocket_1.default.createServer(connection => {
        //连接客户端
        const player = onOpen(connection);
        //收到消息监听
        connection.on('text', function (result) {
            onMessage(player, result);
        });
        //掉线监听
        connection.on('close', function (code) {
            console.log("掉线：close");
            onClose(connection);
        });
        //掉线监听
        connection.on('error', function (code) {
            console.log("掉线：error");
            onClose(connection);
        });
    }).listen(8082);
};
exports.createServer = createServer;
/**
 * 客户端连接
 */
function onOpen(connection) {
    const player = new Player_1.Player(id_num < 10000 ? id_num++ : 1, connection);
    all_players.add(player);
    exports.arr_hallPlayer.add(player);
    const obj = { other: "playerId", id: player.id };
    sendObj(connection, obj);
    numOfHallPlayers();
    return player;
}
/**
 * 收到消息监听
 */
function onMessage(player, msg) {
    const obj = JSON.parse(msg);
    if (obj.other != null) {
        //大厅操作
        switch (obj.other) {
            case "index":
                if (!exports.arr_hallPlayer.has(player)) {
                    exports.arr_hallPlayer.add(player);
                    numOfHallPlayers();
                }
                break;
            case "check_version":
                player.checkVersion(obj.version);
                break;
            case "matching_cancel":
                player.cancel();
                break;
            case "matching":
                player.matching(obj);
                break;
            default: console.error("未知other: ", obj.other);
        }
    }
    else {
        //大厅
        const room = exports.map_room.get(player.roomId);
        room && room.onMessage(obj);
    }
}
/**
 * 客户端掉线
 */
function onClose(connection) {
    all_players.forEach(plyer => {
        if (plyer.connection == null || plyer.connection == connection) {
            all_players.delete(plyer);
            exports.arr_hallPlayer.delete(plyer);
            plyer.dropLine();
            console.log("玩家掉线：" + plyer.id);
            numOfHallPlayers();
        }
    });
}
/**
 * 通知大厅玩家在线人数
 */
function numOfHallPlayers() {
    const obj = { other: "numOfHallPlayers", numOfHallPlayers: exports.arr_hallPlayer.size };
    exports.arr_hallPlayer.forEach(player => {
        sendObj(player.connection, obj);
    });
    console.log("当前大厅在线人数：" + obj.numOfHallPlayers);
}
exports.numOfHallPlayers = numOfHallPlayers;
/**
 * 发送obj
 */
function sendObj(connection, obj) {
    if (connection != null) {
        connection.sendText(JSON.stringify(obj));
    }
}
exports.sendObj = sendObj;
/**
 * 创建Room
 */
function createRoom(type) {
    return new Room_1.Room(roomId_num < 10000 ? roomId_num++ : 1, type);
}
exports.createRoom = createRoom;
